#include "CDashboard_MCB.h"

#include "core/CDeviceManager.h"
#include "core/logger.h"

#define DEFINE_DEVICE()                                                        \
    auto deviceid = property("deviceid").toString().toStdString();             \
    auto device = CDeviceManager::Instance().GetMonitorControlBoard(deviceid); \
    if (device == nullptr)                                                     \
    {                                                                          \
        MSG(ERROR) << QObject::tr("Operation failed, invalid device object");  \
        return;                                                                \
    }

CDashboard_MCB::CDashboard_MCB(QWidget *parent) :
    QWidget(parent)
{
    ui.setupUi(this);

    ResetData();
}

CDashboard_MCB::~CDashboard_MCB()
{
}

void CDashboard_MCB::ResetData()
{
    ui.edt_GetFirmwareVersion->clear();
    ui.edt_GetUpsPowerSupply->clear();
    ui.edt_GetTemperature->clear();
    ui.edt_GetDHT11TemperatureHumidity->clear();
    ui.edt_SetTemperatureThreshold_th->clear();
    ui.edt_SetHighTemperatureAlarmPoint->clear();
    ui.edt_SetTemperatureThreshold_tl->clear();
    ui.edt_SetLowTemperatureAlarmPoint->clear();
    ui.edt_GetLampStatus->clear();
    ui.edt_GetMasterDoorStatus->clear();
    ui.edt_GetFanSpeed->clear();
    ui.edt_GetFanStatus->clear();
    ui.edt_SetCompressorStartingTemperature->clear();
    ui.edt_SetCompressorReturnTemperature->clear();
    ui.edt_SetHeaterStartingTemperature->clear();
    ui.edt_SetHeaterReturnTemperature->clear();
    ui.edt_GetTemperatureProbeStatus_temphalarm->clear();
    ui.edt_GetTemperatureProbeStatus_probalarm->clear();
    ui.edt_heater_th->clear();
    ui.edt_heater_tl->clear();
    ui.edt_heater_env->clear();
    ui.edt_heater_status->clear();
    ui.edt_GetHeaterFirmwareVersion->clear();
    ui.edt_GetDeliveryBoxDoorStatus->clear();
    ui.edt_IsDeliveryBoxFull->clear();
    ui.edt_GetDeliveryBoxNumber->clear();
    ui.edt_PlcPowerTemperature->clear();
}

void CDashboard_MCB::on_btn_GetFirmwareVersion_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetFirmwareVersion->setText("");

    char version[256] {0x00};
    auto ret = device->GetFirmwareVersion(version);
    MSG(INFO) << tr("GetFirmwareVersion") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetFirmwareVersion->setText(version);
}

void CDashboard_MCB::on_btn_ResetSystem_clicked()
{
    DEFINE_DEVICE();
    auto ret = device->ResetSystem();
    MSG(INFO) << tr("ResetSystem") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_ResetDcbAndIpc_clicked()
{
    DEFINE_DEVICE();
    auto ret = device->ResetDcbAndIpc();
    MSG(INFO) << tr("ResetDcbAndIpc") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_ResetDcb_clicked()
{
    DEFINE_DEVICE();
    auto ret = device->ResetDcb();
    MSG(INFO) << tr("ResetDcb") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetUpsPowerSupply_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetUpsPowerSupply->setText("");

    unsigned char supply = 0x00;
    auto ret = device->GetUpsPowerSupply(supply);
    MSG(INFO) << tr("GetUpsPowerSupply") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    QString tmp = supply == 0x00 ? tr("UPS") : supply == 0x01 ? tr("Mains") : tr("Unknown");
    ui.edt_GetUpsPowerSupply->setText(tmp);
}

void CDashboard_MCB::on_btn_GetTemperature_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetTemperature->setText("");

    int32_t temperature = 0;
    auto ret = device->GetTemperature(temperature);
    MSG(INFO) << tr("GetTemperature") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetTemperature->setText(QString::number(float(temperature) / 100.00, 'f', 2));
}

void CDashboard_MCB::on_btn_SetTemperatureThreshold_clicked()
{
    DEFINE_DEVICE();

    int32_t th = ui.edt_SetTemperatureThreshold_th->text().toFloat() * 100;
    int32_t tl = ui.edt_SetTemperatureThreshold_tl->text().toFloat() * 100;
    auto ret = device->SetTemperatureThreshold(th, tl);
    MSG(INFO) << tr("SetTemperatureThreshold") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetTemperatureThreshold_clicked()
{
    DEFINE_DEVICE();

    ui.edt_SetTemperatureThreshold_th->setText("");
    ui.edt_SetTemperatureThreshold_tl->setText("");

    int32_t th(0), tl(0);
    auto ret = device->GetTemperatureThreshold(th, tl);
    MSG(INFO) << tr("GetTemperatureThreshold") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_SetTemperatureThreshold_th->setText(QString::number(float(th) / 100.00, 'f', 2));
    ui.edt_SetTemperatureThreshold_tl->setText(QString::number(float(tl) / 100.00, 'f', 2));
}

void CDashboard_MCB::on_btn_EnableFan_true_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableFan(0x01);
    MSG(INFO) << tr("EnableFan") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_EnableFan_false_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableFan(0x00);
    MSG(INFO) << tr("DisableFan") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetFanSpeed_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetFanSpeed->setText("");

    int32_t speed = 0;
    auto ret = device->GetFanSpeed(speed);
    MSG(INFO) << tr("GetFanSpeed") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetFanSpeed->setText(QString::number(speed));
}

void CDashboard_MCB::on_btn_GetFanStatus_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetFanStatus->setText("");

    int32_t speed = 0;
    unsigned char status = 0x00;
    auto ret = device->GetFanStatus(speed, status);
    MSG(INFO) << tr("GetFanStatus") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetFanStatus->setText(status == 0x00 ? tr("Abnormal") : tr("Normal"));
}

void CDashboard_MCB::on_btn_EnableLamp_0x00_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableLamp(0x00);
    MSG(INFO) << tr("EnableLamp 0x00") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_EnableLamp_0x01_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableLamp(0x01);
    MSG(INFO) << tr("EnableLamp 0x01") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_EnableLamp_0x02_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableLamp(0x02);
    MSG(INFO) << tr("EnableLamp 0x02") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_EnableLamp_0x03_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableLamp(0x03);
    MSG(INFO) << tr("EnableLamp 0x03") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetLampStatus_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetLampStatus->setText("");

    unsigned char status;
    auto ret = device->GetLampStatus(status);
    MSG(INFO) << tr("GetLampStatus") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetLampStatus->setText(status == 0x00 ? tr("enabled") : status == 0x01 ? tr("disabled") : tr("unknown"));
}

void CDashboard_MCB::on_btn_EnableDrawer_true_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableDrawer(0x01);
    MSG(INFO) << tr("EnableDrawer") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_EnableDrawer_false_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableDrawer(0x00);
    MSG(INFO) << tr("DisableDrawer") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetDeliveryBoxDoorStatus_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetDeliveryBoxDoorStatus->setText("");

    unsigned char status = 0x00;
    auto ret = device->GetDeliveryBoxDoorStatus(status);
    MSG(INFO) << tr("GetDeliveryBoxDoorStatus") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetDeliveryBoxDoorStatus->setText(status == 0x00 ? tr("opened") : tr("closed"));
}

void CDashboard_MCB::on_btn_SetDeliveryBoxDoorStatus_true_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->SetDeliveryBoxDoorStatus(0x01);
    MSG(INFO) << tr("SetDeliveryBoxDoorStatus 0x00") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_SetDeliveryBoxDoorStatus_false_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->SetDeliveryBoxDoorStatus(0x00);
    MSG(INFO) << tr("SetDeliveryBoxDoorStatus 0x01") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_EnableDeliveryBoxNumberCheck_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableDeliveryBoxNumberCheck();
    MSG(INFO) << tr("EnableDeliveryBoxNumberCheck") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_DisableDeliveryBoxNumberCheck_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->DisableDeliveryBoxNumberCheck();
    MSG(INFO) << tr("DisableDeliveryBoxNumberCheck") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetDeliveryBoxNumber_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetDeliveryBoxNumber->setText("");

    int32_t count = 0;
    auto ret = device->GetDeliveryBoxNumber(count);
    MSG(INFO) << tr("GetDeliveryBoxNumber") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetDeliveryBoxNumber->setText(QString::number(count));
}

void CDashboard_MCB::on_btn_IsDeliveryBoxFull_clicked()
{
    DEFINE_DEVICE();

    ui.edt_IsDeliveryBoxFull->setText("");

    unsigned char isfull(0x01);
    auto ret = device->IsDeliveryBoxFull(isfull);
    MSG(INFO) << tr("IsDeliveryBoxFull") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_IsDeliveryBoxFull->setText(isfull == 0x00 ? tr("full") : tr("not full"));
}

void CDashboard_MCB::on_btn_OpenArmAndPlcPower_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->OpenArmAndPlcPower();
    MSG(INFO) << tr("OpenArmAndPlcPower") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_CloseArmAndPlcPower_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->CloseArmAndPlcPower();
    MSG(INFO) << tr("CloseArmAndPlcPower") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_SetArmAndPlcReportCmd_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->SetArmAndPlcReportCmd();
    MSG(INFO) << tr("SetArmAndPlcReportCmd") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_SetPlcPowerTemperature_clicked()
{
    DEFINE_DEVICE();

    int32_t temperature = ui.edt_PlcPowerTemperature->text().toFloat() * 100;
    auto ret = device->SetPlcPowerTemperature(temperature);
    MSG(INFO) << tr("SetPlcPowerTemperature") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetPlcPowerTemperature_clicked()
{
    DEFINE_DEVICE();

    ui.edt_PlcPowerTemperature->setText("");

    int32_t temperature = 0;
    auto ret = device->GetPlcPowerTemperature(temperature);
    MSG(INFO) << tr("GetPlcPowerTemperature") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_PlcPowerTemperature->setText(QString::number(float(temperature) / 100.00, 'f', 2));
}

void CDashboard_MCB::on_btn_GetMasterDoorStatus_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetMasterDoorStatus->setText("");

    unsigned char status = 0x00;
    auto ret = device->GetMasterDoorStatus(status);
    MSG(INFO) << tr("GetMasterDoorStatus") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetMasterDoorStatus->setText(status == 0x00 ? tr("closed") : tr("opened"));
}

void CDashboard_MCB::on_btn_OpenMasterDoor_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->OpenMasterDoor();
    MSG(INFO) << tr("OpenMasterDoor") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_OpenLock_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->OpenLock();
    MSG(INFO) << tr("OpenLock") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetDHT11TemperatureHumidity_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetDHT11TemperatureHumidity->setText("");

    int32_t temperature(0), humidity(0);
    auto ret = device->GetDHT11TemperatureHumidity(temperature, humidity);
    MSG(INFO) << tr("GetDHT11TemperatureHumidity") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetDHT11TemperatureHumidity->setText(tr("T:%1, H: %2")
                                                    .arg(QString::number(float(temperature) / 100.00, 'f', 2))
                                                    .arg(QString::number(float(humidity) / 100.00, 'f', 2)));
}

void CDashboard_MCB::on_btn_SetHighTemperatureAlarmPoint_clicked()
{
    DEFINE_DEVICE();

    int32_t temperature = ui.edt_SetHighTemperatureAlarmPoint->text().toFloat() * 100;
    auto ret = device->SetHighTemperatureAlarmPoint(temperature);
    MSG(INFO) << tr("SetHighTemperatureAlarmPoint") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_SetLowTemperatureAlarmPoint_clicked()
{
    DEFINE_DEVICE();

    int32_t temperature = ui.edt_SetLowTemperatureAlarmPoint->text().toFloat() * 100;
    auto ret = device->SetLowTemperatureAlarmPoint(temperature);
    MSG(INFO) << tr("SetLowTemperatureAlarmPoint") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_SetCompressorStartingTemperature_clicked()
{
    DEFINE_DEVICE();

    int32_t t = ui.edt_SetCompressorStartingTemperature->text().toFloat() * 100;
    auto ret = device->SetCompressorStartingTemperature(t);
    MSG(INFO) << tr("SetCompressorStartingTemperature") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_SetCompressorReturnTemperature_clicked()
{
    DEFINE_DEVICE();

    int32_t t = ui.edt_SetCompressorReturnTemperature->text().toFloat() * 100;
    auto ret = device->SetCompressorReturnTemperature(t);
    MSG(INFO) << tr("SetCompressorReturnTemperature") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_SetHeaterStartingTemperature_clicked()
{
    DEFINE_DEVICE();

    int32_t t = ui.edt_SetHeaterStartingTemperature->text().toFloat() * 100;
    auto ret = device->SetHeaterStartingTemperature(t);
    MSG(INFO) << tr("SetHeaterStartingTemperature") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_SetHeaterReturnTemperature_clicked()
{
    DEFINE_DEVICE();

    int32_t t = ui.edt_SetHeaterReturnTemperature->text().toFloat() * 100;
    auto ret = device->SetHeaterReturnTemperature(t);
    MSG(INFO) << tr("SetHeaterReturnTemperature") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetTemperatureProbeStatus_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetTemperatureProbeStatus_temphalarm->setText("");
    ui.edt_GetTemperatureProbeStatus_probalarm->setText("");

    unsigned char ta(0x00), pa(0x00);
    auto ret = device->GetTemperatureProbeStatus(ta, pa);
    MSG(INFO) << tr("GetTemperatureProbeStatus") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    auto pfunc = [](unsigned char status) -> QString {
        if (status == 0x00)
            return tr("normal");
        else if (status == 0x01)
            return tr("abnormal");
        else if (status == 0xFF)
            return tr("invalid status");

        return tr("unknown");
    };

    ui.edt_GetTemperatureProbeStatus_temphalarm->setText(pfunc(ta));
    ui.edt_GetTemperatureProbeStatus_probalarm->setText(pfunc(pa));
}

void CDashboard_MCB::on_btn_SetHeaterCriticalTemperature_clicked()
{
    DEFINE_DEVICE();

    int32_t th = ui.edt_heater_th->text().toFloat() * 100;
    int32_t tl = ui.edt_heater_tl->text().toFloat() * 100;
    auto ret = device->SetHeaterCriticalTemperature(th, tl);
    MSG(INFO) << tr("SetHeaterCriticalTemperature") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetHeaterOption_clicked()
{
    DEFINE_DEVICE();

    ui.edt_heater_th->setText("");
    ui.edt_heater_tl->setText("");
    ui.edt_heater_env->setText("");
    ui.edt_heater_status->setText("");

    zl::zlbase::HeaterOption opt;
    opt.temperature_lower_limit = 0;
    opt.temperature_upper_limit = 0;
    opt.temperature_env = 0;
    opt.status = 0x01;
    auto ret = device->GetHeaterOption(opt);
    MSG(INFO) << tr("GetHeaterOption") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_heater_th->setText(QString::number(float(opt.temperature_upper_limit) / 100.00, 'f', 2));
    ui.edt_heater_tl->setText(QString::number(float(opt.temperature_lower_limit) / 100.00, 'f', 2));
    ui.edt_heater_env->setText(QString::number(float(opt.temperature_env) / 100.00, 'f', 2));
    ui.edt_heater_status->setText(opt.status == 0x00 ? tr("working") : tr("closed"));
}

void CDashboard_MCB::on_btn_GetHeaterEnviromentTemperature_clicked()
{
    DEFINE_DEVICE();

    ui.edt_heater_env->setText("");

    int32_t t(0);
    auto ret = device->GetHeaterEnviromentTemperature(t);
    MSG(INFO) << tr("GetHeaterEnviromentTemperature") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_heater_env->setText(QString::number(t / 100));
}

void CDashboard_MCB::on_btn_EnableHeater_true_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableHeater(0x01);
    MSG(INFO) << tr("EnableHeater") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_EnableHeater_false_clicked()
{
    DEFINE_DEVICE();

    auto ret = device->EnableHeater(0x00);
    MSG(INFO) << tr("DisableHeater") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }
}

void CDashboard_MCB::on_btn_GetHeaterFirmwareVersion_clicked()
{
    DEFINE_DEVICE();

    ui.edt_GetHeaterFirmwareVersion->setText("");

    char version[256] {0x00};
    auto ret = device->GetHeaterFirmwareVersion(version);
    MSG(INFO) << tr("GetHeaterFirmwareVersion") << CRet(ret);
    if (ret != zl::zlbase::ZLResult_Success)
    {
        return;
    }

    ui.edt_GetHeaterFirmwareVersion->setText(version);
}
